package com.jxj.study.controller.Ehcaches;

import com.jxj.study.util.EhcacheUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/ehcaches")
public class EhcachesController {

    @RequestMapping("/showUser")
    public String toIndex(HttpServletRequest request) {
        System.out.println("UserController showUser");
        int id = Integer.parseInt(request.getParameter("id"));

        //将查询到的结果放入缓存
        EhcacheUtil.getInstance().put("myCache", "userEch", id);
        return "showUser";
    }

    @RequestMapping("/getUser")
    public String toEhcahe(HttpServletRequest request, Model model) {
        System.out.println("use cacher");

          //从缓存中取数据
        Object s = EhcacheUtil.getInstance().get("myCache", "userEch");
        model.addAttribute("userCache", EhcacheUtil.getInstance().get("myCache", "userEch"));
        model.addAttribute("user", s);
        return "showUser s == " + s;
    }

}
